package com.ims.common.email;

import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import org.apache.log4j.Logger;



public class EMail {

	private static Logger log = Logger.getLogger(EMail.class);
	private static final String MAIL_SEND_MESSAGE = "Mail  has been successfully sent to -----> ";
	private static final String MAIL_SEND_EXCEPTION_MESSAGE = "Exception in Mail ";
	private static final String ATTACHMENT_MAIL_SEND_EXCEPTION = "Error in Attachment ";

	public static String sendEMail(String hostname, String toAddr,
			String fromAdd, String subject, String message, String ccAddr) {
		String msgText1 = message;
		String mailDispatchedStatus = null;
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", hostname);
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(fromAdd));
			InternetAddress address[] = InternetAddress.parse(toAddr);
			InternetAddress ccaddress[] = InternetAddress.parse(ccAddr);
			msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
			msg.setRecipients(javax.mail.Message.RecipientType.CC, ccaddress);
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(subject);
			msg.setContent(msgText1, "text/html");
			Transport.send(msg);
			mailDispatchedStatus = MAIL_SEND_MESSAGE + toAddr;
			log.info(mailDispatchedStatus);
		} catch (Exception e) {
			log.error(MAIL_SEND_EXCEPTION_MESSAGE, e);
		}
		return mailDispatchedStatus;
	}

	public static String sendEmailAttach(String hostname, String toAddr,
			String fromAdd, String subject, String message, String ccAddr,
			String[] attachment) {

		String msgText1 = message;
		String[] attachmentFilePath = attachment;
		String mailDispatchedStatus = null;
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", hostname);
			Session session = Session.getDefaultInstance(props, null);
			MimeMessage msg = new MimeMessage(session);

			msg.setFrom(new InternetAddress(fromAdd));

			InternetAddress address[] = InternetAddress.parse(toAddr);
			InternetAddress ccaddress[] = InternetAddress.parse(ccAddr);

			msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
			msg.setRecipients(javax.mail.Message.RecipientType.CC, ccaddress);
			msg.setRecipients(Message.RecipientType.TO, address);
			msg.setSubject(subject);

			MimeMultipart mimemultipart = new MimeMultipart("relative");

			/*
			 * following try/catch block to do multiple file attachment in mail
			 */

			try {
				for (int i = 0; i < attachmentFilePath.length; i++) {
					Boolean flag = false;
					MimeBodyPart mimebodypart2 = new MimeBodyPart();
					try {
						FileDataSource filedatasource1 = new FileDataSource(
								attachmentFilePath[i]);
						mimebodypart2.setDataHandler(new DataHandler(
								filedatasource1));
						mimebodypart2.setFileName(filedatasource1.getName());
					} catch (Exception e) {
						flag = true;
						log.error(ATTACHMENT_MAIL_SEND_EXCEPTION, e);
					}
					if (!flag) {
						mimemultipart.addBodyPart(mimebodypart2);
					}
				}
			} catch (Exception e) {
				log.error(ATTACHMENT_MAIL_SEND_EXCEPTION, e);
			}

			/*
			 * Mail Body Construts
			 */

			Multipart mailBody = new MimeMultipart();
			MimeBodyPart mainBody = new MimeBodyPart();
			DataHandler dh = new DataHandler(msgText1, "text/html");
			mainBody.setDataHandler(dh);

			/*
			 * attach the body to the MainBody.
			 */

			mailBody.addBodyPart(mainBody);

			mimemultipart.addBodyPart(mainBody);

			msg.setContent(mimemultipart);
			Transport.send(msg);
			mailDispatchedStatus = MAIL_SEND_MESSAGE + toAddr;
			log.info(mailDispatchedStatus);

		} catch (Exception e) {
			log.error(MAIL_SEND_EXCEPTION_MESSAGE, e);
		}
		return mailDispatchedStatus;
	}
 
}

